package controller.expertInfo.adminEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.ExpertBaseServices;
import util.IdCardUtil;
import validator.*;
import wt.entity.expertInfo.ExpertBase;
import wt.staticData.ShortData;

import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("expertInfo/adminEdit/expertBase")
public class ExpertBaseController extends BaseController {

    @Autowired
    ExpertBaseServices expertBaseServices;


    @RequiresPermissions("expertInfo:adminEdit:show")
    @RequestMapping("index")
    public String index(String id, Map map) throws Exception {
        map.put("data", expertBaseServices.selectByPrimaryKey(id));
        return "expertInfo/adminEdit/expertBase/index";
    }

    @RequiresPermissions("expertInfo:adminEdit:edit")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, Map map) throws Exception {
        if (id != null) {
            ExpertBase expertBase;

            expertBase = expertBaseServices.selectByPrimaryKey(id);

            if (expertBase == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, expertBase);
            map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertBase/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertBase/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "expertInfo/adminEdit/expertBase/editDlg";
    }

    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ExpertBase expertBase) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertBase.getName(), new NotBlankFluentValidator("姓名"))
                .on(expertBase.getIdCard(), new IdCardFluentValidator("身份证号码"))
                .on(expertBase.getPhone(), new MobileFluentValidator("手机号码"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }

        expertBase.setBirthday(IdCardUtil.getBirthByIdCard(expertBase.getIdCard()));
        expertBase.setSex(IdCardUtil.getGenderByIdCard(expertBase.getIdCard())==1?ShortData.s1:ShortData.s0);



       ExpertBase base= expertBaseServices.selectByIdCard(expertBase.getIdCard());
        if (base!=null){
            commonResult.setMessage("身份证号码已存在！");
            return commonResult.setError();
        }else{
            expertBase.setLive(ShortData.s1);
            expertBaseServices.adminInsertSelective(expertBase);
            return commonResult.setSuccess();
        }


    }


    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertBase expertBase) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertBase.getName(), new NotBlankFluentValidator("姓名"))
                .on(expertBase.getIdCard(), new IdCardFluentValidator("身份证号码"))
                .on(expertBase.getPhone(), new MobileFluentValidator("手机号码"))
                .on(expertBase.getBirthday(), new NotNullFluentValidator("出生日期"))
                .on(expertBase.getSex(), new NotNullFluentValidator("性别"))
                .on(expertBase.getBankCard(), new NotBlankFluentValidator("银行卡号"))
                .on(expertBase.getBankName(), new NotBlankFluentValidator("开户行"))
                .on(expertBase.getEmail(), new EmailFluentValidator("电子邮件"))
                .on(expertBase.getEducation(), new NotNullFluentValidator("学历"))
                .on(expertBase.getRetire(), new NotNullFluentValidator("状态"))
                .on(expertBase.getTeacher(), new NotNullFluentValidator("是否老师"))
                .on(expertBase.getOutLander(), new NotNullFluentValidator("是否省外"))
                .on(expertBase.getRank(), new NotNullFluentValidator("职称"))
                .on(expertBase.getLive(), new NotNullFluentValidator("是否启用"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        //防止图片被错误覆盖 图标地址长度都大于30
        if (expertBase.getHeadImg() != null && expertBase.getHeadImg().length() < 30) {
            expertBase.setHeadImg(null);
        }
        expertBaseServices.adminUpdateByPrimaryKeySelective(expertBase);
        return commonResult.setSuccess();
    }









}
